package com.leisai.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leisai.manager.common.utils.DateUtils;
import com.leisai.manager.dao.TempMapper;
import com.leisai.manager.dto.TempDTO;
import com.leisai.manager.pojo.Temp;
import com.leisai.manager.response.PageDataResult;
import com.leisai.manager.service.TempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TempServiceImpl implements TempService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private TempMapper tempMapper ;
    @Override
    public Map<String, Object> addTemp(Temp temp) {
        Map<String,Object> data = new HashMap();
        try {
            temp.setVersionNo(DateUtils.getFormatTime(new Date() , "yyyyMMddHHmmss"));
            temp.setStatus(1);
            if (temp.getSort() == null){
                Integer maxSort = tempMapper.getMaxSort();
                temp.setSort(maxSort+1);
            }
            int result = tempMapper.insertTemp(temp);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("模板[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("模板[新增]，结果=新增成功！");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("模板[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updateTemp(Temp temp) {
        Map<String,Object> data = new HashMap();
        try {
            temp.setVersionNo(DateUtils.getFormatTime(new Date() , "yyyyMMddHHmmss"));
            temp.setStatus(1);
            int result = tempMapper.updateTemp(temp);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("模板[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("模板[更新]，结果=更新成功！");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("模板[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public PageDataResult getTempList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();

        PageHelper.startPage(pageNum, pageSize);
        List<TempDTO> tempDTOS = tempMapper.getTempList();

        if(tempDTOS.size() != 0){
            PageInfo<TempDTO> pageInfo = new PageInfo<>(tempDTOS);
            pageDataResult.setList(tempDTOS);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> del(Long id) {
        Map<String ,Object> data = new HashMap<>();
        try{
            int result = tempMapper.deleteByPrimaryKey(id);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除失败");
                logger.error("删除失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除成功");
            logger.info("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除模板异常！", e);
        }
        return data;
    }

    @Override
    public Temp getTempById(Long id) {
        return tempMapper.selectByPrimaryKey(id);
    }


    @Override
    public Map<String, Object> setStatus(Temp temp) {
        Map<String,Object> data = new HashMap<>();
        try {
            int result = tempMapper.setStatus(temp);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除失败");
                logger.error("删除失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除成功");
            logger.info("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("更改报表状态失败！");
        }
        return data;
    }


}
